package cn.jtfadmin.core.modules.sys.service.impl;


import cn.jtfadmin.base.dict.dto.DictItemModelDTO;
import cn.jtfadmin.base.dict.dto.DictModelDTO;
import cn.jtfadmin.base.dict.service.DynamicDictConfig;
import cn.jtfadmin.base.lang.common.service.BaseTreeService;
import cn.jtfadmin.core.constant.OrgTypeConstant;
import cn.jtfadmin.core.modules.sys.domain.entity.SysDepartEntity;
import cn.jtfadmin.core.modules.sys.service.SysDepartActionService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import java.util.*;

/**
 * 系统部门服务实现, 部门的增删改查
 *
 * @author jtf
 * @since 1.0
 */
@Service
@Validated
@AllArgsConstructor
public class SysDepartActionServiceImpl implements SysDepartActionService, DynamicDictConfig {



    protected BaseTreeService<SysDepartEntity> service;

    @Override
    public SysDepartEntity findParentForType(SysDepartEntity sysDepart, Integer type) {
        if (type.equals(sysDepart.getOrgType())) {
            return sysDepart;
        }
        String parentId = sysDepart.getParentId();
        if (StringUtils.hasText(parentId)) {
            Optional<SysDepartEntity> optional = service.findById(parentId);
            if (optional.isPresent()) {
                return findParentForType(optional.get(),type);
            }
        }
        return sysDepart;
    }



    @Override
    public Set<String> getDictCode() {
        Set<String> set = new HashSet<>();
        set.add(OrgTypeConstant.ORG_TYPE_DICT_CODE);
        return set;
    }

    @Override
    public List<DictModelDTO> getInitDict() {
        DictModelDTO dto = new DictModelDTO(
                OrgTypeConstant.ORG_TYPE_DICT_CODE, "机构类型",
                Arrays.asList(
                        new DictItemModelDTO(OrgTypeConstant.ORG_TYPE_COMPANY.toString(), OrgTypeConstant.ORG_TYPE_COMPANY_NAME),
                        new DictItemModelDTO(OrgTypeConstant.ORG_TYPE_DEPART.toString(), OrgTypeConstant.ORG_TYPE_DEPART_NAME)
                )
        );
        return Arrays.asList(dto);
    }


}
